/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-14
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description:  路由页面
 */
import 'package:chat_client/screens/chat/chat_page.dart' show ChatScreen;
import 'package:chat_client/screens/mobile/menu/aplly_list.dart';
import 'package:chat_client/utils/route_tools.dart';
import 'package:flutter/material.dart';

/// mobile端
import 'package:chat_client/screens/mobile/home_screen.dart';
//
import 'package:chat_client/screens/mobile/login/register.dart';
import 'package:chat_client/screens/mobile/login/login_screen.dart';
//

// web端
import 'package:chat_client/screens/web/home_screen.dart';
import 'package:chat_client/screens/web/login_screen.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

class RouteManager {
  static final RouteManager _instance = RouteManager._internal();

  factory RouteManager() => _instance;

  RouteManager._internal();

  // 定义路由
  static final Map<String, WidgetBuilder> routes = {
    // 移动端
    '/mobile-home': (context) => MobileHomeScreen(),
    '/mobile-login': (context) => MobileLoginScreen(),
    '/mobile-register': (context) => RegisterPage(),
    '/mobile-contact-preview': (context) => RegisterPage(),
    '/mobile-friend-request': (context) => FriendApplicationScreen(),

    // Web端
    '/web-home': (context) => WebHomeScreen(),
    '/web-login': (context) => WebLoginScreen(),
  };

  // 压入路由记录,带参数（并放置压入相同路由）
  static void navigateTo(String routeName, {Object? arguments}) {
    // 获取当前路由名称
    final currentRoute =
        ModalRoute.of(navigatorKey.currentContext!)?.settings.name;
    // 检查是否重复（路径名和参数均相同）
    if (currentRoute == routeName) return;

    navigatorKey.currentState?.pushNamed(
      routeName,
      arguments: arguments,
    );
  }

  // 格式'chat/groupId:16155154'或'chat/userId:565562'
  static void navigateWithPayload(String payload) {
    final currentRoute = ModalRoute.of(navigatorKey.currentContext!);
    print('navigateWithPayloadnavigateWithPayload-:$currentRoute');
    // 如果是转跳Chat页面
    if (validateChat(payload)) {
      navigatorKey.currentState?.push(MaterialPageRoute(
        builder: (context) => ChatScreen(pathStr: payload),
        settings: RouteSettings(
          name: 'chat', // 固定名称或动态生成唯一标识
          arguments: payload, // 携带参数
        ),
      ));
    } else {
      navigatorKey.currentState?.pushNamed(payload);
    }
  }

  // 在目标页面的 build 方法中获取参数
  //   class MobileHomeScreen extends StatelessWidget {
  //   @override
  //   Widget build(BuildContext context) {
  //     final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>?;
  //     final userId = args?['userId'];
  //     return Scaffold(...);
  //   }
  // }

  // 返回
  static void goBack() {
    navigatorKey.currentState?.pop();
  }

  // 替换当前路由（清除当前路由栈顶）,带参数
  // 示例：RouteManager.replaceWithArguments('/detail', arguments: {'id': 123})
  static void replaceWith(String routeName, {Object? arguments}) {
    navigatorKey.currentState?.pushReplacementNamed(
      routeName,
      arguments: arguments,
    );
  }

  // 直接传递 Widget 实例，无需配置路由表
  static void navigateToWidget(Widget page, {Object? arguments}) {
    navigatorKey.currentState?.push(
      MaterialPageRoute(
        builder: (context) => page,
        settings: RouteSettings(arguments: arguments), // 可选参数传递
      ),
    );
  }
}
